var i;

function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}

function liste_muscle ()
{
	var select_type = document.getElementById("type_m"+i);
	var select_list = document.getElementById("liste_m"+i);
	
	var http = getXMLHttpRequest();
	
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			select_list.innerHTML = http.responseText;	
		}
	};	
	
	http.open('post', 'actions_enseignants/ajout_atelier.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "action=liste&type="+select_type.value;		
	http.send(data);
}	

function update_profil()
{
   
	var http = getXMLHttpRequest();
  var div = document.getElementById("modif_profil_form");
	var nom = document.getElementById("nom").value;
	var prenom = document.getElementById("prenom").value;
	var genre = document.getElementById("genre").value;
	var login = document.getElementById("login").value;
  var email = document.getElementById("email").value;
	var mot_de_passe = document.getElementById("mot_de_passe").value;
  var mot_de_passe_new = document.getElementById("mot_de_passe_new").value;
	
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			div.innerHTML = http.responseText;	
		}
	};
 
	http.open('post', 'actions_enseignants/fiche_enseignant_action.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "async_update=1&nom="+nom+"&prenom="+prenom+"&genre="+genre+"&email="+email+"&login="+login+"&mot_de_passe="+mot_de_passe+"&mot_de_passe_new="+mot_de_passe_new;
  http.send(data);
}

function ajout_atelier( )
{
	var nom = document.getElementById("nom_atelier");
	var div = document.getElementById("error_form");
	var desc = document.getElementById("desc_atelier");
  var consigne = document.getElementById("consigne_securite");
	var create = document.getElementById("create");
	
	var liste_m = document.getElementById("liste_m0");
	var muscle_m = document.getElementById("type_m0");
	var ajout = document.getElementById("ajout0");  
	var button_end = document.getElementById("end");
	
	var http = getXMLHttpRequest();	
	
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
        var response = http.responseText;
        var erreur = response.indexOf("Erreur",0);
				div.innerHTML = response;
        

       if( erreur < 0 )
				{
				
        	consigne.disabled = true;
          nom.disabled = true;
					desc.disabled = true;	
					create.disabled = true;
          
					ajout.disabled = false;
					liste_m.disabled = false;
          muscle_m.disabled = false;
					button_end.disabled = false;
	
  				i = 0;
					liste_muscle();
				}	
			}		
	};
  var test = "atelier";
	http.open('post', 'actions_enseignants/ajout_atelier.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "nom="+nom.value+"&desc="+desc.value+"&consigne="+consigne.value+"&action=atelier";		
	http.send(data);
}

function ajouter_champs_muscle()
{
	var div = document.getElementById("insertion");
	var button = document.getElementById("ajout"+i);
	var liste_m = document.getElementById("liste_m"+i);
	var type_m = document.getElementById("type_m"+i);
	var div_error = document.getElementById("error_form");
	var atelier_nom = document.getElementById("nom_atelier");
	var musle = liste_m.value;
	var http = getXMLHttpRequest();
	var muscle = liste_m.value;
	
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			div_error.innerHTML = http.responseText;	

			if( http.responseText.indexOf("Erreur",0) < 0 )
			{
			  button.type = "hidden";
			  liste_m.options[liste_m.selectedIndex].defaultSelected = true;
			  liste_m.disabled = true;
			  type_m.disabled = true;
				liste_m.options[0].value = muscle;
			  i++;
				div.innerHTML = div.innerHTML+"<tr><td><SELECT name='type"+i+"' id='type_m"+i+"' onChange='javascript:liste_muscle()'><OPTION VALUE='muscle'>Muscle</OPTION><OPTION VALUE='groupe_musculaire'>Groupe Musculaire</OPTION></SELECT></td><td><SELECT name='liste"+i+"' id='liste_m"+i+"' ></SELECT></td><td><input type='button' name='add"+i+"' value='Ajouter' id='ajout"+i+"' onClick='javascript:ajouter_champs_muscle()'/></td></tr>";
			  liste_muscle();
			}
		}
	};	
	
	http.open('post', 'actions_enseignants/ajout_atelier.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "action=muscle&type="+type_m.value+"&nom="+atelier_nom.value+"&id="+liste_m.value;		
	http.send(data);
}

function fin_ajout_muscle()
{
	var div_error = document.getElementById("error_form");
	var liste_m = document.getElementById("liste_m"+i);
	var type_m = document.getElementById("type_m"+i);
	var button = document.getElementById("ajout"+i);
	var button_validate = document.getElementById("end");
	
	div_error.innerHTML = "<div class='success_div'>Ajout de l'atelier et des muscles effectue avec succes.</div>";
	liste_m.disabled = true;
	type_m.disabled = true;
	button.disabled = true;
	button_validate.disabled = true;

	setTimeout("window.location.replace('index_enseignant.php');",2500);	
}	

function ajouter_eleve()
{
	var http = getXMLHttpRequest();
  var div = document.getElementById("erreur_ajout_eleve");
	var nom_eleve = document.getElementById("nom_eleve").value;
	var prenom_eleve = document.getElementById("prenom_eleve").value;
	var genre_eleve = document.getElementById("genre_eleve").value;
	var classe_eleve = document.getElementById("classe_eleve").value;
	var login_eleve = document.getElementById("login_eleve").value;
	var mot_de_passe_eleve = document.getElementById("mot_de_passe_eleve").value;
	var confirm_passe_eleve = document.getElementById("confirm_passe_eleve").value;
	
	http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			div.innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/ajout_eleve.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "nom_eleve="+nom_eleve+"&prenom_eleve="+prenom_eleve+"&genre_eleve="+genre_eleve+"&classe_eleve="+classe_eleve+"&login_eleve="+login_eleve+"&mot_de_passe_eleve="+mot_de_passe_eleve+"&confirm_passe_eleve="+confirm_passe_eleve;		
	http.send(data);
}

function liste_bilan_eleve ()
{
  var id_eleve = document.getElementById("eleve");
  var div_liste_bilan = document.getElementById("liste_bilan");
  var button_validate = document.getElementById("end");
  var div_error = document.getElementById("error_div");
  
  var http = getXMLHttpRequest();
  
  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
		  var response = http.responseText;
      var erreur = response.indexOf("Erreur",0);
			if( erreur < 0 )
			{
					id_eleve.disabled = true;
					button_validate.disabled = true;	
					div_liste_bilan.innerHTML = http.responseText;			
      }else	
        div_error.innerHTML = http.responseText;
		}
	};
	http.open('post', 'actions_enseignants/liste_bilan.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "type=eleve&id_eleve="+id_eleve.value;		
	http.send(data);
}

function retour_bilan_eleve(type)
{
  if(type == 1)
  {
    var id_eleve = document.getElementById("eleve");
    var div_liste_bilan = document.getElementById("liste_bilan");
    var button_validate = document.getElementById("end");
    
    id_eleve.disabled = false;
    button_validate.disabled = false;
    div_liste_bilan.innerHTML = "";
  }
  else
  {
    if(type == 2)
    {
      var date_seance = document.getElementById("date_seance"); 
      var date_bouton = document.getElementById("date_bouton");
      var choix_date_bouton = document.getElementById("choix_date_bouton");
      var bilan_liste_date = document.getElementById("bilan_liste_date");
      
      date_seance.disabled = false;
      date_bouton.disabled = false;
      choix_date_bouton.disabled = false;
      
      bilan_liste_date.innerHTML = "";
    }
  }
}

function seance_detail(id)
{
  var div_detail = document.getElementById("detail_seance");
  
  var http = getXMLHttpRequest();

  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
		   	div_detail.innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/liste_bilan.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "type=bilan&id_seance="+id;		
	http.send(data);
}

function seance_date()
{
  var choix_date_bouton = document.getElementById("choix_date_bouton");
  var champs_date = document.getElementById("date_seance");
  var choix_date_bouton = document.getElementById("choix_date_bouton"); 
  var date_bouton = document.getElementById("date_bouton");
  
  var div_error = document.getElementById("div_error");
  var bilan_liste_date = document.getElementById("bilan_liste_date");
 
  var http = getXMLHttpRequest(); 
  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
      var response = http.responseText;
      var erreur = response.indexOf("Erreur",0);
			if( erreur < 0 )
			{
        choix_date_bouton.disabled = true;
        champs_date.disabled = true;
        date_bouton.disabled = true;
        bilan_liste_date.innerHTML = http.responseText;	
        div_error.innerHTML = "";		
      }else	
        div_error.innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/liste_bilan.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "type=date&date="+champs_date.value;		
	http.send(data);
}

function liste_atelier_evol()
{
  var eleve = document.getElementById("eleve");
  var button_ok = document.getElementById("end");
  
  var div_error = document.getElementById("error_div");
  var div_evolution = document.getElementById("div_evolution");
 
  var http = getXMLHttpRequest(); 
  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
      var response = http.responseText;
      var erreur = response.indexOf("Erreur",0);
			if( erreur < 0 )
			{
        eleve.disabled = true;
        button_ok.disabled = true;
        div_evolution.innerHTML = http.responseText;	
        div_error.innerHTML = "";		
      }else	
        div_error.innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/evolution_graph.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "type=liste&id_eleve="+eleve.value;		
	http.send(data);
}

function evolution_valide(){
  var atelier = document.getElementById("atelier");
  var eleve = document.getElementById("eleve");
    
  var div_error = document.getElementById("error_div");
  var div_graph = document.getElementById("graph");
  div_graph.innerHTML = "";
 
  var http = getXMLHttpRequest(); 
  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
      var response = http.responseText;
      var erreur = response.indexOf("Erreur",0);
		if( erreur < 0 )
		{
        div_graph.innerHTML = http.responseText;	
        div_error.innerHTML = "";		
        
      }else	
        div_error.innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/evolution_graph.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "type=graph&id_atelier="+atelier.value+"&id_eleve="+eleve.value;		
	http.send(data);  
}

function evolution_retour() {
  var eleve = document.getElementById("eleve");
  var button_ok = document.getElementById("end");
  
  var div_error = document.getElementById("error_div");
  var div_evolution = document.getElementById("div_evolution");
 
  eleve.disabled = false;
  button_ok.disabled = false;
  div_evolution.innerHTML = "";
}

function terminer_cycle(confirmation)
{
  if( confirmation )
  {
    var http = getXMLHttpRequest(); 
    http.onreadystatechange = function()
    {
      if (http.readyState == 4)
      {
        document.getElementById("reponse_div").innerHTML = http.responseText;	
      }
    };
    http.open('post', 'actions_enseignants/terminer_cycle.php', true);
    http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    http.send();  
  }
  else
  {
    window.location.replace("index_enseignant.php");
  }
}

function maj_right(id_enseignant)
{
  var http = getXMLHttpRequest(); 
  http.onreadystatechange = function()
  {
    if (http.readyState == 4)
    {
      document.getElementById("admin_enseignant").innerHTML = http.responseText;	
    }
  };
  http.open('post', 'actions_enseignants/admin_enseignant.php', true);
  http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  http.send("update_right=1&id_enseignant="+id_enseignant);  
}

function ajouter_professeur()
{
	var http = getXMLHttpRequest();
  
  var div = document.getElementById("erreur_ajout_prof");
	var nom_prof = document.getElementById("nom_professeur").value;
	var prenom_prof = document.getElementById("prenom_professeur").value;
	var genre_prof = document.getElementById("genre_professeur").value;
	var email = document.getElementById("email").value;
	var login_prof = document.getElementById("login_professeur").value;
	var mot_de_passe_prof = document.getElementById("mot_de_passe_professeur").value;
	var confirm_passe_prof = document.getElementById("confirm_passe_prof").value;
  var admin = document.getElementById("admin").checked;

  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			div.innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/admin_enseignant.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "insert_enseignant=1&admin="+admin+"&nom_prof="+nom_prof+"&prenom_prof="+prenom_prof+"&genre_prof="+genre_prof+"&email="+email+"&login_prof="+login_prof+"&mot_de_passe_prof="+mot_de_passe_prof+"&confirm_passe_prof="+confirm_passe_prof;		
	http.send(data);
}

function voir_fiche_atelier()
{
	var http = getXMLHttpRequest();
  var id_atelier = document.getElementById("id_atelier").value;

  http.onreadystatechange = function()
	{
		if (http.readyState == 4)
		{
			document.getElementById("details_atelier").innerHTML = http.responseText;			
		}
	};
	http.open('post', 'actions_enseignants/fiche_atelier.php', true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	data = "voir_fiche=1&id_atelier="+id_atelier;		
	http.send(data);
}
